package com.edgclub.edg.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.edgclub.edg.R;
import com.edgclub.edg.activity.GzTopicDetailActivity;
import com.edgclub.edg.model.Topic;
import com.edgclub.edg.utils.GlideUtil.GlideUtils;

import java.util.ArrayList;
import java.util.List;

public class OtherUserTopicAdapter extends BaseAdapter{
    private Context mContext;
    private List<Topic>list;
    GlideUtils glideUtils=new GlideUtils();
    public OtherUserTopicAdapter(Context Context) {
        super();
        mContext = Context;
    }

    public void setList(List<Topic> list) {
        this.list = list;
        if(null==this.list)this.list=new ArrayList<>();
    }

    @Override
    public int getCount() {
        if(list.size()>3){
            return 3;
        }else{
            return list.size();
        }
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (null == convertView) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.item_othre, null);
            holder = new ViewHolder();
            holder.icon_iv = (ImageView) convertView.findViewById(R.id.icon_iv_top);
            holder.content = (TextView) convertView.findViewById(R.id.content);
            convertView.setTag(R.id.tag_first,holder);
        }else {
            holder = (ViewHolder) convertView.getTag(R.id.tag_first);
        }

        if(null!=list.get(position).getAlbums()&&list.get(position).getAlbums().size()>0){
            String url=list.get(position).getAlbums().get(0).getUrl()+"";
            holder.icon_iv.setVisibility(View.VISIBLE);
            if(url.contains("gz://audio")){
                holder.icon_iv.setVisibility(View.GONE);
                if("".equals(list.get(position).getContent())) {
                    holder.content.setText("[语音]");
                }else{
                    holder.content.setText(list.get(position).getContent());
                }
            }else if(url.contains("video://")){
                if("".equals(list.get(position).getContent())) {
                    glideUtils.Glidde_loadRoudImage(mContext,list.get(position).getAlbums().get(0).getThumbnail()+"",holder.icon_iv,10);
                    holder.content.setText("无正文内容");
                }else {
                    glideUtils.Glidde_loadRoudImage(mContext, list.get(position).getAlbums().get(0).getThumbnail() + "", holder.icon_iv, 10);
                    holder.content.setText(list.get(position).getContent());
                }
                }else{
                if("".equals(list.get(position).getContent())) {
                    glideUtils.Glidde_loadRoudImage(mContext,url,holder.icon_iv,10);
                    holder.content.setText("无正文内容");
                }else{
                    glideUtils.Glidde_loadRoudImage(mContext,url,holder.icon_iv,10);
                    holder.content.setText(list.get(position).getContent());
                }



//                glideUtils.Glidde_loadRoudImage(mContext,url,holder.icon_iv,10);
//                holder.content.setText(list.get(position).getContent());
            }
        }else{
            holder.content.setText(list.get(position).getContent());
            holder.icon_iv.setVisibility(View.GONE);
        }
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(mContext, GzTopicDetailActivity.class);
                intent.putExtra("topicId",list.get(position).getPostsId()+"");
                mContext.startActivity(intent);
            }
        });
        return convertView;
    }
    class ViewHolder {
        private ImageView icon_iv;
        private TextView content;
    }
}
